﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YunSpace.Core;
using YunSpace.Domain.Entity;
using YunSpace.Domain.Entity.SystemManage;
using YunSpace.Domain.IRepository.OrderFlowManage;
using YunSpace.Domain.IRepository.SystemManage;
using YunSpace.Domain.ViewModel.User;
using YunSpace.Repository.OrderFlowManage;

namespace YunSpace.Application.SystemManage
{
    public class MessageNoticeApp
    {
        private readonly IMessageNoticeRepository _messageNoticeRepository = new MessageNoticeRepository();
        private readonly UserApp _userApp = new UserApp();
        private readonly OrganizeApp _organizeApp = new OrganizeApp();
        private readonly DutyApp _dutyApp = new DutyApp();
        private static readonly Dictionary<string, string> UserIdToNameMaps = new Dictionary<string, string>();

        #region ctor

        static MessageNoticeApp()
        {
            InitCacheData();
        }

        #endregion ctor

        /// <summary>
        /// 获取未读消息
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public UnreadMessageViewModel GetUnreadMessage(string keyValue)
        {
            var messageNotice = _messageNoticeRepository.FindEntity(keyValue);
            UserIdToNameMaps.TryGetValue(messageNotice.F_CreatorUserId, out var userName);

            var vm = new UnreadMessageViewModel()
            {
                F_CreatorTime = messageNotice.F_CreatorTime.ToString(),
                F_CreatorUser = userName,
                F_MessageContent = messageNotice.F_MessageContent,
                F_MessageType = messageNotice.F_MessageType.IsEmpty() ? "" : (messageNotice.F_MessageType == 1 ? "审核通知" : "申请通知"),
            };

            return vm;
        }

        /// <summary>
        /// 获取最新消息
        /// </summary>
        /// <returns></returns>
        public MessageNoticeEntity GetNewestUnreadMessage()
        {
            return GetUnreadMessages().FirstOrDefault();
        }

        /// <summary>
        /// 获取未读信息列表
        /// </summary>
        /// <returns></returns>
        public List<MessageNoticeEntity> GetUnreadMessages()
        {
            //符合全部人通知消息类型或接收人编号
            //重复、已读、删除的过滤

            var currentProvider = OperatorProvider.Provider.GetCurrent();

            if (currentProvider == null)
            {
                return new List<MessageNoticeEntity>();
            }

            var unreadMessages = _messageNoticeRepository.IQueryable(x =>
                (x.F_DeleteMark == null || x.F_DeleteMark == false) &&
                x.F_ReceiveUserId == currentProvider.UserId && x.F_IsRead.Value != 1).OrderByDescending(x=>x.F_CreatorTime).ToList();

            return unreadMessages;
        }

        /// <summary>
        /// 设置消息为已读
        /// </summary>
        /// <param name="keyValue"></param>
        public void SetMessageForReaded(string keyValue)
        {
            var unreadMessage = _messageNoticeRepository.FindEntity(x => x.F_Id == keyValue);
            unreadMessage.F_IsRead = 1;
            unreadMessage.Modify(keyValue);

            _messageNoticeRepository.Update(unreadMessage);
        }

        #region private static method

        private static void InitCacheData()
        {
            var userApp = new UserApp();
            var users = userApp.GetUsers("");

            foreach (var user in users)
            {
                UserIdToNameMaps.Add(user.F_Id, user.F_RealName);
            }
        }

        #endregion private static method


    }
}
